package com.ia.aistream.manager.operating.util.convert;

import com.ia.aistream.common.utils.StringUtils;
import com.ia.aistream.common.utils.spring.SpringUtils;
import com.ia.aistream.manager.operating.util.DocRegularUtil;
import com.ia.aistream.mapper.system.SysDictDataMapper;
import com.ia.aistream.model.operating.dto.convert.ConvertDto;
import com.ia.aistream.model.operating.dto.convert.EquOperInfo;
import com.ia.aistream.model.operating.dto.convert.LocationInfo;
import com.ia.aistream.model.operating.dto.convert.SystemEquInfo;
import com.ia.aistream.model.system.entity.SysDictData;
import com.spire.doc.TableCell;
import com.spire.doc.documents.Paragraph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
 * 处理表格中需要特殊含义的字符
 * @author XiaoYu
 */
public class DocHandleBusiness {

    /***
     * 获取段落的转义内容
     * @param paragraph
     * @return
     */
    public static ConvertDto getParagraphConvert(Paragraph paragraph){
        // 获取段落的内容
        String trim = paragraph.getText().trim();
        ConvertDto convertDto = new ConvertDto();
        List<String> list = handleContentEquipment(trim);
        if(list!=null && list.size()>0){
            convertDto.setEq(getSystemEquInfo(list));
        }
        if(trim.startsWith("注意")){
            convertDto.setZy(trim);
        }
        if(trim.startsWith("重要提示")){
            convertDto.setZyts(trim);
        }

        // 处理数据为空的情况
        if("{}".equals(convertDto.toString())){
            return null;
        }
        return convertDto;
    }

    /**
     * 获取单元格转义后的结果
     * @param tableCell
     * @return
     */
    public static ConvertDto getTableConvertContent(TableCell tableCell, int underLine){

        // 创建转义后对象
        ConvertDto convertDto = new ConvertDto();
        // 获取转义后的内容
        String tableCellContent = getTableCellContent(tableCell);
        // 判断是否为重要提示
        if(tableCellContent.startsWith("重要提示")){
            convertDto.setZyts(tableCellContent);
            return convertDto;
        }

        // 判断是否为重要风险提示
        if(tableCellContent.startsWith("重要风险提示")){
            convertDto.setZyfxts(tableCellContent);
            return convertDto;
        }

        // 判断是否为签名信息
        if(tableCellContent.startsWith("签字：")){
            convertDto.setQm("签字");
            return convertDto;
        }
        // 处理房间数据
        LocationInfo location = getLocation(tableCellContent);
        if(location!=null){
            convertDto.setLocationInfo(location);
            return convertDto;
        }

        //判断是否含有功能组
        if(tableCellContent.contains("功能组")){
            // 处理功能组
            String group = getFunctionalGroup(tableCellContent);
            if(!StringUtils.isEmpty(group)){
                convertDto.setGnz(group);
                tableCellContent = tableCellContent.replace(group, " ");
            }

        }
        // 获取字典表对象
        SysDictDataMapper sysDictDataMapper = SpringUtils.getBean("sysDictDataMapper");

        // 获取操作数据
        List<SysDictData> doc_op = sysDictDataMapper.selectDictDataByType("doc_op");
        List<String> oplist = new ArrayList<>();
        for (SysDictData d : doc_op){
            oplist.add(d.getDictValue());
        }

        // 获取参数字典表
        List<SysDictData> doc_args = sysDictDataMapper.selectDictDataByType("doc_args");
        List<String> argList = new ArrayList<>();
        for(SysDictData d : doc_args){
            argList.add(d.getDictValue());
        }

        // 表示逻辑运算符字典表
        List<SysDictData> doc_logicOp = sysDictDataMapper.selectDictDataByType("doc_logicOp");
        List<String> logicOp = new ArrayList<>();
        for(SysDictData d : doc_logicOp){
            if(d.getDictValue().equals("&gt;")){
                logicOp.add(">");
                continue;
            }
            logicOp.add(d.getDictValue());
        }

        // 判断单元格内容是否为空，为空则直接返回null
        if(StringUtils.isEmpty(tableCellContent)){
            return null;
        }

        List<String> eq = new ArrayList<>();
        // 处理含有逻辑操作的数据
        EquOperInfo equOperInfo = geteqOpContent(oplist, argList, logicOp, tableCellContent);

        tableCellContent = tableCellContent.replace("或", "/");
        if(DocRegularUtil.isNumOther(tableCellContent)){
            tableCellContent = tableCellContent.replace("、", "/");
        }
        // 处理系统设备编码含有‘-’和‘/’分割的情况,将设备按照、拼接
        if(DocRegularUtil.isNeedEquipment(tableCellContent)){
            if(tableCellContent.contains("-")){
                List<String> list = handleHorizontalBar(tableCellContent);
                eq.addAll(list);
            }else{
                List<String> list = handleSlantingBar(tableCellContent);
                eq.addAll(list);
            }
        }else if(DocRegularUtil.isTwoEqu(tableCellContent)){
            List<String> twoEqu = getTwoEqu(tableCellContent);
            eq.addAll(twoEqu);
        }else {
            if(equOperInfo==null || StringUtils.isEmpty(equOperInfo.getParam())){
                // 处理段落中的系统
                List<String> list = handleContentEquipment(tableCellContent);
                if(list!=null && list.size()>0){
                    eq.addAll(list);
                }
            }
        }

        // 如果不为空则给逻辑操作赋值
        if(equOperInfo!=null && !StringUtils.isEmpty(equOperInfo.getParam())){
            convertDto.setEq_op(equOperInfo);
        }

        // 如果不为空则给设备赋值
        if(eq.size()>0){
            convertDto.setEq(getSystemEquInfo(eq));
        }

        // 处理单元格中含有的注意
        if(tableCellContent.contains("注意")){
            String careFul = getCareFul(tableCell);
            if(!StringUtils.isEmpty(careFul)){
                convertDto.setZy(careFul);
            }
        }

        // 处理需要填写的内容下划线
        if(underLine>0){
            convertDto.setRefill(String.valueOf(underLine));
        }

        // 判断是否为空
        if("{}".equals(convertDto.toString())){
            return null;
        }
        return convertDto;

    }

    /***
     * 获取功能组后的数据
     * @param msg
     * @return
     */
    private static String getFunctionalGroup(String msg){
        // 截取功能组后的内容
        int index = msg.indexOf("功能组");

        int start = 0;
        int end = msg.length();
        // 判断字符串的截取位置
        if(index-10>=start){
            start = index-10;
        }
        if(index+10 < end){
            end = index + 10;
        }
        String sub  = msg.substring(start, end);
        String s="";

        for (int i=0; i<sub.length(); i++){
            char charAt = sub.charAt(i);
            if(isNum(charAt) || isLetter(charAt) || charAt==' '){
                s = s+charAt;
            }
        }

        // 判断结果是否符合设备
        if(DocRegularUtil.getEquipment(s)){
            return s;
        }

        return null;
    }

    public static void main(String[] args) {
        String s = "LN0";
        String ss = " 用KU调节下列调节阀的位置以免流量过大（每台泵不超过100m3/小时）";
// 表示逻辑运算符字典表
        List<String> logicOp = new ArrayList<>();
        logicOp.add("左右");
        logicOp.add("到达");
        logicOp.add("约");
        logicOp.add("接近");
        logicOp.add("≤");
        logicOp.add("<");
        logicOp.add("=");
        logicOp.add(">=");
        logicOp.add("＞");
        logicOp.add("＜");
        logicOp.add("~");
        logicOp.add("=");
        logicOp.add("不超过");


        // 表示参数字典表
        List<String> argList = new ArrayList<>();
        argList.add("m3/h");
        argList.add("MPa");
        argList.add("Mpa");
        argList.add("℃");
        argList.add("us/cm");
        argList.add("ug/L");
        argList.add("圈");
        argList.add("%");
        argList.add("bar.a");
        argList.add("bar");
        argList.add("KPa.g");
        argList.add("KPa");
        argList.add("kPa");
        argList.add("（红刻度线以上）");
        argList.add("A");
        argList.add("mm/s");
        argList.add("mm");
        argList.add("μg/kg");
        argList.add("mm／sec");
        argList.add("m³/h");
        argList.add("m3/小时");

        List<String> oplist = new ArrayList<>();
        oplist.add("开启");
        oplist.add("关闭");
        oplist.add("开度");
        oplist.add("调节");
        oplist.add("启动");
        oplist.add("达到");
        oplist.add("加压");
        System.out.println(geteqOpContent(oplist, argList, logicOp, ss));
    }
    /***
     * 获取位置的信息
     * @param msg
     * @return
     */
    public static LocationInfo getLocation(String msg){

        LocationInfo locationInfo = new LocationInfo();
        String[] split = msg.split("_");
        for(String s: split){
            // 处理位置信息
            if(DocRegularUtil.isLocation(s)){
                locationInfo.setRoom(s);
            }else if(DocRegularUtil.isTwoLetter(s)){
                locationInfo.setWorkshop(s);
            }else if(DocRegularUtil.isCrewLocation(s)){
                String crewWorkshop = getCrewWorkshop(s);
                String elevation = getElevation(s);
                String room = getRoom(s);
                if(!StringUtils.isEmpty(crewWorkshop)){
                    if(crewWorkshop.length()>2){
                        locationInfo.setCrew(crewWorkshop.substring(0,1));
                        locationInfo.setWorkshop(crewWorkshop.substring(1));
                    }else{
                        locationInfo.setWorkshop(crewWorkshop);
                    }
                }

                if(!StringUtils.isEmpty(elevation)){
                    locationInfo.setElevation(elevation);
                }

                if(!StringUtils.isEmpty(room)){
                    locationInfo.setRoom(room);
                }
            }
        }

        if(locationInfo.getCrew()!=null || locationInfo.getElevation()!=null || locationInfo.getRoom()!=null || locationInfo.getWorkshop()!=null){
            return locationInfo;
        }
        return null;
    }

    /**
     * 将设备编码拆分为四部分
     * @param msg
     * @return
     */
    private static List<SystemEquInfo> getSystemEquInfo(List<String> msg){
        if(msg==null || msg.size()==0){
            return null;
        }
        List<SystemEquInfo> list = new ArrayList<>();
        for(String s:msg){
            SystemEquInfo systemEquInfo = new SystemEquInfo();
            String s1 = s.replace(" ", "");
            if(s1.length()>=8 || s1.length()<5){
                // 处理机组和系统
                String crewSystem = getCrewSystem(s);

                if(!StringUtils.isEmpty(crewSystem)){
                    if(crewSystem.length()>3){
                        systemEquInfo.setCrew(crewSystem.substring(0,1));
                        systemEquInfo.setSystem(crewSystem.substring(1).replace(" ", ""));
                    }else{
                        systemEquInfo.setSystem(crewSystem);
                    }
                }
            }

            // 处理设备
            String equip = getEquip(s);
            if(!StringUtils.isEmpty(equip)){
                systemEquInfo.setEquip(equip.replace(" ", ""));
                // 处理部件编码
                if(s.length()>11){
                    int i = s.indexOf(equip);
                    String replace = s.substring(i + equip.length()).replace(" ", "");
                    if(!StringUtils.isEmpty(replace)){
                        systemEquInfo.setComponent(replace);
                    }
                }
            }
            list.add(systemEquInfo);
        }

        // 处理部件
        if (list.size()>0){
            return list;
        }
        return null;
    }

    /***
     * 获取内容转义后的数据
     * @param opList
     * @param argList
     * @param logicOp
     * @param s
     */
    public static EquOperInfo geteqOpContent(List<String> opList, List<String> argList, List<String> logicOp,String s){

        boolean flag = false;
        // 记录参数
        List<String> index = new ArrayList<>();
        // 记录操作
        String opStr = null;

        // 处理操作集合
        for(int i=0; i<opList.size(); i++){
            if(opList.get(i).equals("调节") && s.contains("调节阀")){
                continue;
            }
            if(s.contains(opList.get(i))){
                flag = true;
                opStr = opList.get(i);
                break;
            }
        }

        // 处理参数单位集合
        for (int i=0; i< argList.size(); i++){
            String a = argList.get(i);

            if(s.contains(a)){
                if(DocRegularUtil.containsLetter(a)){
                    String unit = getUnit(argList, s, a);
                    if(!StringUtils.isEmpty(unit) && !index.contains(unit)){
                        index.add(unit);
                        flag = true;
                    }
                }else{
                    index.add(a);
                    flag = true;
                }
            }
        }

        // 处理满足的情况
        if(flag){

            EquOperInfo equOperInfo = new EquOperInfo();

            List<SystemEquInfo> systemEquInfo = getSystemEquInfo(handleContentEquipment(s));
            if(systemEquInfo!=null && systemEquInfo.size()>0){
                equOperInfo.setEqSystemList(systemEquInfo);
            }
            String unit = "";
            if(index.size()>0){
                unit = index.get(0);
            }
            // 处理2<P<15KPa的情况
            if(DocRegularUtil.isMiddleParam(s)){
                equOperInfo.setParam(getMiddleParam(s, unit));
                equOperInfo.setLogicOp("<");
            }else if(DocRegularUtil.isMiddleUnit(s)){
                // 处理0.2-0.5MPa的情况
                equOperInfo.setParam(getMiddleUnit(s, unit));
                equOperInfo.setLogicOp("-");
            }else{
                // 处理逻辑运算符
                for(int i=0; i<logicOp.size(); i++){
                    if(s.contains(logicOp.get(i))){
                        equOperInfo.setLogicOp(logicOp.get(i));
                        break;
                    }
                }

//                if (!StringUtils.isEmpty(opStr) && !"开度".equals(opStr)){
//                    s = s.substring(s.indexOf(opStr));
//                }
                // 获取获取参数
                String param = "";
                // 处理设备参数
                for (String arg : index){
                    String substring = s.substring(0, s.indexOf(arg));
                    int i=0;
                    String result = "";
                    for(i=substring.length()-1; i>=0; i--){
                        char c = substring.charAt(i);
                        if(c >= '0' && c<='9'){
                            result = c + result;
                        }else if(c=='.' || c=='/' || c=='-' || c==' ' || c=='±'){
                            result = c + result;
                        }else {
                            if(DocRegularUtil.handleNum(result)){
                                param = param + "、" + result + arg;
                            }
                            break;
                        }
                    }
                    if(i==-1 && DocRegularUtil.handleNum(result)){
                        param = param + "、" + result + arg;
                    }
                }
                if(!StringUtils.isEmpty(param)){
                    param = param.substring(1).trim();
                    // 处理含有±类的情况
                    if(param.contains("±")){
                        param = addAndSub(param);
                        equOperInfo.setLogicOp("≤");
                    }
                    equOperInfo.setParam(param);
                }
                if(equOperInfo.getParam()!=null && equOperInfo.getLogicOp()==null){
                    equOperInfo.setLogicOp("=");
                }
                if(equOperInfo.getParam()!=null){
                    // 获取操作
                    equOperInfo.setOp(opStr);
                }
            }

            return equOperInfo;
        }else if(DocRegularUtil.isInterval(s)){
            String op = "";
            EquOperInfo equOperInfo = new EquOperInfo();
            if(s.contains("-")){
                op = "-";
            }else{
                op = "~";
            }
            String[] split = s.split(op);
            String param =split[0] + "、" + split[1];
            equOperInfo.setParam(param);
            equOperInfo.setLogicOp(op);

            return equOperInfo;
        }

        return null;
    }


    /***
     * 处理逻辑运算符为±的数据
     * @param msg
     * @return
     */
    private static String addAndSub(String msg){
        msg = msg.replace(" ", "");
        String[] split = msg.split("±");
        String num = "";
        String unit = "";
        // 获取数字
        for(int i=0; i<split[1].length(); i++){
            char charAt = split[1].charAt(i);
            if(isNum(charAt)){
                num = num + charAt;
            }else{
                unit = split[1].substring(i);
                break;
            }
        }

        // 获取小的数据
        int min = Integer.parseInt(split[0]) - Integer.parseInt(num);
        int max = Integer.parseInt(split[0]) + Integer.parseInt(num);
        // 获取大的数据
        return min + unit + "、" + max + unit;
    }
    /***
     * 获取字符串中的单位
     * @param list 参数集合
     * @msg 原始数据
     * @param uStr 单位
     * @return
     */
    private static String getUnit(List<String> list, String msg, String uStr){

        String result = "";
        String substring = msg.substring(msg.indexOf(uStr));
        if(uStr.equals("m3/小时")){
            return  uStr;
        }
        for(int i=0; i<substring.length(); i++){
            char charAt = substring.charAt(i);
            if(isLetter(charAt) || isNum(charAt) || charAt=='.' || charAt=='/' || charAt=='／' || charAt=='℃' || charAt=='³' || charAt=='μ'){
                result = result+ charAt;
            }else{
                break;
            }
        }
        for(String s:list){
            if(result.equals(s)){
                return s;
            }
        }

        return null;
    }


    /***
     * 获取2<P<15kPa数据
     * @param msg
     * @param unit
     * @return
     */
    private static String getMiddleParam(String msg, String unit){
        String[] split = msg.split("<");
        String pre = split[0];
        String after = split[split.length-1];
        String numPre = "";
        String numAfter = "";
        // 处理前边的内容
        for(int i=pre.length()-1; i>=0; i--){
            if(isNum(pre.charAt(i))){
                numPre = pre.charAt(i) + numPre;
            }else{
                break;
            }
        }
        // 处理后边的内容
        for(int i=0; i<after.length()-1; i++){
            if (isNum(after.charAt(i))){
                numAfter = numAfter + after.charAt(i);
            }else{
                break;
            }
        }
        String result = numPre+unit + "、" + numAfter + unit;
        return result;
    }

    /***
     *  获取0.2-0.9MPa相似的情况
     * @param msg
     * @param unit
     * @return
     */
    private static String getMiddleUnit(String msg, String unit){
        String[] split = msg.split("-");
        String pre = split[0];
        String after = split[split.length-1];
        String numPre = "";
        String numAfter = "";
        // 处理前边的内容
        for(int i=pre.length()-1; i>=0; i--){
            char charAt = pre.charAt(i);
            if(isNum(charAt)|| charAt=='.'){
                numPre = charAt + numPre;
            }else{
                break;
            }
        }
        // 处理后边的内容
        for(int i=0; i<after.length()-1; i++){
            char charAt = after.charAt(i);
            if (isNum(charAt) || charAt=='.'){
                numAfter = numAfter + charAt;
            }else{
                break;
            }
        }
        String result = numPre+unit + "、" + numAfter + unit;
        return result;
    }

    /***
     * 获取带有 “-” 的内容
     * @param msg
     * @return
     */
    public static List<String> handleHorizontalBar(String msg){

        String[] s = msg.split("_");
        List<String> list = new ArrayList<>();
        for(String cellContent : s){
            StringBuilder result = new StringBuilder();
            if(cellContent.contains("-")) {
                String numPre = "";
                String numAfter = "";
                boolean flag = true;
                boolean flagAfter = true;
                String textPre = "";
                String textAfter = "";
                cellContent = cellContent.replace( " ", "");

                String subPrex = cellContent.substring(0, cellContent.indexOf("-"));
                String subAfter = cellContent.substring(cellContent.indexOf("-")+1);

                // 处理-之前的内容
                for(int i=subPrex.length()-1; i>=0; i--){
                    char charAt = subPrex.charAt(i);
                    if(flag && isNum(charAt)){
                        numPre = charAt + numPre;
                    }else if(isLetter(charAt) || isNum(charAt)){
                        flag = false;
                        textPre = charAt + textPre;
                    }else{
                        break;
                    }
                }

                // 处理-之后的内容
                for(int i=0; i<subAfter.length(); i++){
                    char charAt = subAfter.charAt(i);
                    if(flagAfter && isNum(charAt)){
                        numAfter = numAfter + subAfter.charAt(i) ;
                    }else if(isLetter(charAt) || isNum(charAt)){
                        flagAfter = false;
                        textAfter = textAfter + subAfter.charAt(i);
                    }else{
                        break;
                    }
                }
                if(StringUtils.isEmpty(numPre) || StringUtils.isEmpty(numAfter)){
                    continue;
                }
                // 进行拼接
                int min = Integer.parseInt(numPre);
                int max = Integer.parseInt(numAfter);
                for(int i=min; i<=max; i++){
                    result.append(textPre);
                    result.append(i);
                    result.append(textAfter);

                    list.add(result.toString());
                    result = new StringBuilder();
                }
            }else{
                List<String> strList = handleContentEquipment(cellContent);
                if(strList!=null && strList.size()>0){
                    list.addAll(strList);
                }
            }
        }
        
        return list;
    }

    /**
     * 处理 “/” 数据
     * @param msg 需要处理的数据
     * @return
     */
    public static List<String> handleSlantingBar(String msg){
        boolean contFlag = DocRegularUtil.isTableCellContent(msg);
        if(contFlag){
            msg = msg.replace("_", "");
        }
        msg = msg.replace(" ", "");
        msg = msg.replaceAll("[\\u4e00-\\u9fa5]", "_");

        if(msg.charAt(0)=='/'){
            msg = msg.substring(1);
        }
        String[] s = msg.split("_");

       List<String> list = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        for(String cellContent: s){
            if(StringUtils.isEmpty(cellContent)){
                continue;
            }

            // 处理1ATE411/1ATE412PO是否为这种情况
            if (isAbnormalEquipment(cellContent)){
                stringBuilder.append(abnEquipment(cellContent));
                stringBuilder.append("、");

            }else if(DocRegularUtil.isOtherEquipment(cellContent)){
                // 处理1CEX320VL/022VD
                stringBuilder.append(getOtherEquipment(cellContent));
                stringBuilder.append("、");
            }else if(DocRegularUtil.isThrewEquipment(cellContent)){
                stringBuilder.append(getThreeEquipment(cellContent));
                stringBuilder.append("、");
            }else if(DocRegularUtil.isNormalEquipment(cellContent)){
                String[] split = cellContent.split("/");
                String strPre = split[0];
                String strAfter = split[split.length-1];
                String numPre = "";
                String numAfter = "";
                boolean flag = true;
                boolean zmFlag = true;
                boolean numFlag = true;
                boolean flagAfter = true;
                String textPre = "";
                String textAfter = "";

                //处理001PO：101/102/103/104MV；情况
                if(strPre.contains("：")){
                    String res = "";
                    String ss = strPre.substring(0, strPre.indexOf("："));
                    for(int i=ss.length()-1; i>=0; i--){
                        char charAt = strPre.charAt(i);
                        if (isNum(charAt) || isLetter(charAt)){
                            res = charAt + res;
                        }else{
                            break;
                        }
                    }
                    if (DocRegularUtil.getEquipment(res.toString())){
                        stringBuilder.append(res.toString());
                        stringBuilder.append("、");
                    }
                }
                // 处理前边的内容
                for(int i=strPre.length()-1; i>=0; i--){
                    char charAt = strPre.charAt(i);
                    if(flag && isNum(charAt)){
                        numPre = charAt + numPre;
                        zmFlag = false;
                    }else if(isLetter(charAt) && zmFlag){
                        numPre = charAt + numPre;
                        numFlag = false;
                    }else if(isLetter(charAt) || isNum(charAt) ){
                        flag = false;
                        textPre = charAt + textPre;
                    }else{
                        break;
                    }
                }

                // 处理后边的内容
                for(int i=0; i<strAfter.length(); i++){
                    char charAt = strAfter.charAt(i);
                    if(flagAfter && isNum(charAt)){
                        numAfter = numAfter + charAt ;
                    }else if (isLetter(charAt) || isNum(charAt)){
                        flagAfter = false;
                        textAfter = textAfter + charAt;
                    }else{
                        break;
                    }
                }
                if(numFlag){
                    stringBuilder.append(textPre);
                    stringBuilder.append(numPre);
                    stringBuilder.append(textAfter);
                    stringBuilder.append("、");

                    if(split.length>2){
                        for(int i=1; i<split.length-1; i++){
                            stringBuilder.append(textPre);
                            stringBuilder.append(split[i]);
                            stringBuilder.append(textAfter);

                            stringBuilder.append("、");
                        }
                    }
                    stringBuilder.append(textPre);
                    stringBuilder.append(numAfter);
                    stringBuilder.append(textAfter);
                    stringBuilder.append("、");
                }else{
                    stringBuilder.append(textPre);
                    stringBuilder.append(numPre);
                    stringBuilder.append("、");

                    stringBuilder.append(textPre);
                    stringBuilder.append(numAfter);
                    stringBuilder.append(textAfter);
                    stringBuilder.append("、");
                }
            }else{
                List<String> list1 = handleContentEquipment(cellContent);
                if(list1!=null && list1.size()>0){
                    list.addAll(list1);
                }
            }

        }
        if(!StringUtils.isEmpty(stringBuilder)){
            String s1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
            String[] split = s1.split("、");
            List<String> list1 = Arrays.asList(split);
            list.addAll(list1);

        }
        return list;
    }

    /***
     * 获取1ATE411/1ATE412PO中的设备
     * @param msg
     * @return
     */
    private static String abnEquipment(String msg){
        StringBuilder stringBuilder = new StringBuilder();
        String[] split = msg.split("/");
        String strPre = split[0];
        String strAfter = split[split.length-1];
        String numPre = "";
        String numAfter = "";
        boolean zmFlag = true;
        String textAfter = "";

        // 处理前边的内容
        for(int i=strPre.length()-1; i>=0; i--){
            char charAt = strPre.charAt(i);
            if(isLetter(charAt) || isNum(charAt)){
                numPre = charAt + numPre;
            }else{
                break;
            }
        }

        // 处理后边的内容
        for(int i=0; i<strAfter.length(); i++){
            char charAt = strAfter.charAt(i);
            if(isNum(charAt)){
                numAfter = numAfter + charAt ;
                if(i>1){
                    zmFlag = false;
                }
            }else if (zmFlag && isLetter(charAt)){
                numAfter = numAfter + charAt ;
            }else if(isLetter(charAt)){
                textAfter = textAfter + charAt;
            }else{
                break;
            }
        }

        stringBuilder.append(numPre);
        stringBuilder.append(textAfter);
        stringBuilder.append("、");

        stringBuilder.append(numAfter);
        stringBuilder.append(textAfter);

        return stringBuilder.toString();
    }

    /***
     * 获取001PO/002/003PO置于
     * @param msg
     * @return
     */
    private static String getThreeEquipment(String msg){
        StringBuilder stringBuilder = new StringBuilder();
        String[] split = msg.split("/");
        String strPre = split[0];
        String strAfter = split[split.length-1];
        String numPre = "";
        String numAfter = "";
        boolean zmFlag = true;
        boolean flag = true;
        String textPre = "";

        if(split.length==2){
            // 处理前边的内容
            for(int i=strPre.length()-1; i>=0; i--){
                char charAt = strPre.charAt(i);
                if(flag && isNum(charAt)){
                    numPre = charAt + numPre;
                    zmFlag = false;
                }else if(zmFlag && isLetter(charAt)){
                    numPre = charAt + numPre;
                }else if(isLetter(charAt) || isNum(charAt)){
                    textPre = charAt + textPre;
                    flag = false;
                }else{
                    break;
                }
            }

            // 处理后边的内容
            for(int i=0; i<strAfter.length(); i++){
                char charAt = strAfter.charAt(i);
                if(isNum(charAt) || isLetter(charAt)){
                    numAfter = numAfter + charAt ;
                }else{
                    break;
                }
            }

            stringBuilder.append(textPre);
            stringBuilder.append(numPre);
            stringBuilder.append("、");

            stringBuilder.append(textPre);
            stringBuilder.append(numAfter);
        }else if(split.length==3){
            String textAfter = "";
            // 处理前边的内容
            for(int i=strPre.length()-1; i>=0; i--){
                char charAt = strPre.charAt(i);
                if(isLetter(charAt) || isNum(charAt)){
                    textPre = charAt + textPre;
                }else{
                    break;
                }
            }

            // 处理后边的内容
            for(int i=0; i<strAfter.length(); i++){
                char charAt = strAfter.charAt(i);
                if(isNum(charAt)){
                    numAfter = numAfter + charAt ;
                }else if(isLetter(charAt)){
                    textAfter = textAfter + charAt;
                }else{
                    break;
                }
            }

            stringBuilder.append(textPre);
            stringBuilder.append("、");
            for (int j=1; j<split.length-1; j++){
                stringBuilder.append(split[j]+textAfter);
                stringBuilder.append("、");
            }
            stringBuilder.append(numAfter);
            stringBuilder.append(textAfter);
        }

        return stringBuilder.toString();
    }

    /***
     * 获取1CEX320VL/022VD中的设备
     * @param msg
     * @return
     */
    private static String getOtherEquipment(String msg){
        StringBuilder stringBuilder = new StringBuilder();
        String[] split = msg.split("/");
        String strPre = split[0];
        String strAfter = split[split.length-1];
        String numPre = "";
        String numAfter = "";
        boolean zmFlag = true;
        boolean flag = true;
        String textPre = "";

        if(split.length==2){
            // 处理前边的内容
            for(int i=strPre.length()-1; i>=0; i--){
                char charAt = strPre.charAt(i);
                if(flag && isNum(charAt)){
                    numPre = charAt + numPre;
                    zmFlag = false;
                }else if(zmFlag && isLetter(charAt)){
                    numPre = charAt + numPre;
                }else if(isLetter(charAt) || isNum(charAt)){
                    textPre = charAt + textPre;
                    flag = false;
                }else{
                    break;
                }
            }

            // 处理后边的内容
            for(int i=0; i<strAfter.length(); i++){
                char charAt = strAfter.charAt(i);
                if(isNum(charAt) || isLetter(charAt)){
                    numAfter = numAfter + charAt ;
                }else{
                    break;
                }
            }

            stringBuilder.append(textPre);
            stringBuilder.append(numPre);
            stringBuilder.append("、");

            stringBuilder.append(textPre);
            stringBuilder.append(numAfter);
        }else if(split.length==3){
            String textAfter = "";
            // 处理前边的内容
            for(int i=strPre.length()-1; i>=0; i--){
                char charAt = strPre.charAt(i);
                if(isLetter(charAt) || isNum(charAt)){
                    textPre = charAt + textPre;
                }else{
                    break;
                }
            }

            // 处理后边的内容
            for(int i=0; i<strAfter.length(); i++){
                char charAt = strAfter.charAt(i);
                if(isNum(charAt)){
                    numAfter = numAfter + charAt ;
                }else if(isLetter(charAt)){
                    textAfter = textAfter + charAt;
                }else{
                    break;
                }
            }

            stringBuilder.append(textPre);
            stringBuilder.append("、");
            for (int j=1; j<split.length-1; j++){
                stringBuilder.append(split[j]+textAfter);
                stringBuilder.append("、");
            }
            stringBuilder.append(numAfter);
            stringBuilder.append(textAfter);
        }

        return stringBuilder.toString();
    }

    /**
     * 获取段落中含有的系统
     * @param cellContent 单元格段落的内容
     * @return
     */
    public static List<String> handleContentEquipment(String cellContent){

        // 去掉字符串中的空格
//        cellContent = cellContent.replace(" ", "");
        String[] split = cellContent.split("_");

        // 存储段落中的对象
        List<String> strList = new ArrayList<>();
        for(String msg: split){
            if(DocRegularUtil.isThreeEquipment(msg)){
                strList.add(msg.replace(" ", ""));
            }else{
                // 判断是否符合该情况101、103、105、107、109、111 RS
                StringBuilder sb = new StringBuilder();
                for(int i=0; i < msg.length(); i++){
                    if(msg.charAt(i)>='0' && msg.charAt(i)<='9'){
                        sb.append(msg.charAt(i));
                    }else if(isLetter(msg.charAt(i)) || msg.charAt(i)==' '){
                        sb.append(msg.charAt(i));
                    }else{
                        String s = DocRegularUtil.handleSystemEquipment(sb.toString());
                        if(!StringUtils.isEmpty(s) && !strList.contains(s.toString())){
                            strList.add(s);
                            sb = new StringBuilder();
                        }else if(DocRegularUtil.getEquipment(sb.toString()) && !strList.contains(sb.toString())){
                            strList.add(sb.toString().replace(" ", ""));
                            sb = new StringBuilder();
                        }else{
                            // 判断是否为机组系统

                            if(DocRegularUtil.isSystem(sb.toString()) &&!strList.contains(sb.toString()) ){
                                String mid = msg;
                                // 获取数据前后的位置
                                int indexOf = mid.indexOf(sb.toString());
                                if(indexOf-5>=0 && indexOf+5<mid.length()){
                                    mid = mid.substring(indexOf-5, indexOf+6);
                                }
                                if(!DocRegularUtil.isFileCode(mid)){
                                    strList.add(sb.toString());
                                }
                            }
                            sb = new StringBuilder();
                        }
                    }
                }
                // 处理 1ATE381LG情况
                String res = DocRegularUtil.handleSystemEquipment(sb.toString());
                if(!StringUtils.isEmpty(res) && !strList.contains(res)){
                    strList.add(res);
                }else if(DocRegularUtil.getEquipment(sb.toString()) && !strList.contains(sb.toString())){
                    strList.add(sb.toString().replace(" ", ""));
                }else if(DocRegularUtil.isSystem(sb.toString()) && !DocRegularUtil.isFileCode(msg)){
                    strList.add(sb.toString());
                }
            }
        }

        if(strList.size()>0){
            return strList;
        }

        return null;
    }

    /**
     * 获取需要注意的内容
     * @param tableCell
     * @return
     */
    private static String getCareFul(TableCell tableCell){

        StringBuilder sb = new StringBuilder();

        boolean flag = false;
        for(int i=0; i<tableCell.getParagraphs().getCount(); i++){
            Paragraph paragraph = tableCell.getParagraphs().get(i);
            String trim = paragraph.getText().trim();
//            boolean bold = paragraph.get(0).getCharacterFormat().getBold();
            if(trim.startsWith("注意") ){
                sb.append(trim);
                flag = true;
            }else if(flag){
                sb.append(trim);
            }
        }

        return sb.toString();
    }

    /***
     * 205 KT—210 KT
     * @param msg
     * @return
     */
    public static List<String> getTwoEqu(String msg){
        List<String> list = new ArrayList<>();
        String[] split = msg.split("—");
        String pre = split[0];
        String after = split[1];
        String numPre = "";
        String numAfter = "";
        String textAfter = "";

        boolean flag = true;
        // 处理前边的内容
        for(int i=pre.length()-1; i>=0; i--){
            char charAt = pre.charAt(i);
            if(isNum(charAt)){
                numPre = charAt + numPre;
                flag = false;
            }else if(flag && isLetter(charAt)){
                continue;
            }else{
                break;
            }
        }
        // 处理后边的内容
        for(int j=0; j<after.length(); j++){
            char charAt = after.charAt(j);
            if(isNum(charAt)){
                numAfter = numAfter + charAt;
            }else if(isLetter(charAt)){
                textAfter = textAfter + charAt;
            }else{
                break;
            }
        }

        // 处理数据
        int i1 = Integer.parseInt(numPre);
        int i2 = Integer.parseInt(numAfter);
        for(int i=i1; i<=i2; i++){
            String s = i + textAfter;
            list.add(s);
        }

        return list;
    }



    /*************************** 私有化工具类方法  *********************************************/
    /***
     * 1ATE411/1ATE412PO是否为这种情况
     * @param msg
     * @return
     */
    private static boolean isAbnormalEquipment(String msg){
        String equPattern = "^.*\\d{2}/\\d{1}[A-Z]+.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }

    /***
     * 获取单元格中所有段落的值，用_隔开
     * @param tableCell
     * @return
     */
    private static String getTableCellContent(TableCell tableCell){
        String result = "";
        for(int i=0; i<tableCell.getParagraphs().getCount(); i++){
            String pContent = tableCell.getParagraphs().get(i).getText().trim();
            if(StringUtils.isEmpty(pContent)){
                continue;
            }
            result = result + "_" + pContent;
        }

        // 去掉第一个横杠
        if(!StringUtils.isEmpty(result)){
            result = result.substring(1);
        }
        return result;
    }

    /***
     * 获取机组
     * @param msg
     * @return
     */
    private static String getCrewSystem(String msg){
        String reg = "\\d?\\s?[A-Z]{3}";
        Pattern p = Pattern.compile(reg);
        Matcher matcher = p.matcher(msg);
        boolean flag = matcher.find();
        if(flag){
            String group = matcher.group();
            return group;
        }

        return null;
    }

    /**
     *  获取设备信息
     * @param msg
     * @return
     */
    private static String getEquip(String msg){
        String reg = "\\d{3,5}\\s?[A-Z]{2,3}[a-z]?\\d?";
        Pattern p = Pattern.compile(reg);
        Matcher matcher = p.matcher(msg);
        boolean flag = matcher.find();
        if(flag){
            String group = matcher.group();
            return group;
        }
        return null;
    }

    /***
     * 获取数据中的设备
     * @param msg
     * @return
     */
    public static List<String> getOtherThree(String msg){
        msg = msg.replace(" ", "");
        List<String> list = new ArrayList<>();
        String num = "";
        String[] split = msg.split("/");
        // 获取数字
        for(int i=0; i<split[0].length(); i++){
            char charAt = split[0].charAt(i);
            if(isNum(charAt)){
                num = num + charAt;
            }else {
                break;
            }
        }
        // 获取字母
        char charAt = split[0].charAt(split[0].length() - 2);
        char charAt2 = split[0].charAt(split[0].length() - 1);
        list.add(num+charAt+charAt2);
        list.add(num + charAt + split[1]);
        return list;
    }

    /***
     * 获取机组厂房数据
     * @param msg
     * @return
     */
    private static String getCrewWorkshop(String msg){
        String reg = "\\d?\\s?[A-Z]{2}";
        Pattern p = Pattern.compile(reg);
        Matcher matcher = p.matcher(msg);
        boolean flag = matcher.find();
        if(flag){
            String group = matcher.group();
            return group;
        }
        return null;
    }

    /**
     * 获取数据中的标高
     * @param msg
     * @return
     */
    private static String getElevation(String msg){
        String reg = "-?\\d+\\.?\\d*m|-?\\d+\\.?\\d*米";
        Pattern p = Pattern.compile(reg);
        Matcher matcher = p.matcher(msg);
        boolean flag = matcher.find();
        if(flag){
            String group = matcher.group();
            return group;
        }
        return null;
    }

    /***
     * 获取房间
     * @param msg
     * @return
     */
    private static String getRoom(String msg){
        String reg = "\\d{3}";
        Pattern p = Pattern.compile(reg);
        Matcher matcher = p.matcher(msg);
        boolean flag = matcher.find();
        if(flag){
            String group = matcher.group();
            return group;
        }
        return null;
    }


    /***************************  通用工具类   *****************************/
    /***
     * 判断字符是不是数字
     * @param num
     * @return
     */
    public static boolean isNum(char num){
        if(num>='0' && num<='9'){
            return true;
        }
        return false;
    }

    /***
     * 判断是否为字母
     * @param msg
     * @return
     */
    public static boolean isLetter(char msg){
        if((msg >='A' && msg<='Z') || (msg>='a' && msg<='z')){
            return true;
        }
        return false;
    }
}
